import {authorizeOSSRenewWX} from "../../../service/api/api-aliyun";

Component({
    properties: {
        //展示图片宽度
        width: {
            type: Number,
            optionalTypes: [String],
            value: 220
        },
        //展示图片高度
        height: {
            type: Number,
            optionalTypes: [String],
            value: 220
        },
        //初始化图片路径
        value: {
            type: Array,
            value: [],
            observer(val) {
                this.initImages()
            }
        },
        //初始化图片id
        idList: {
            type: Array,
            value: [],
        },
        //禁用删除
        forbidDel: {
            type: Boolean,
            value: false
        },
        //禁用添加
        forbidAdd: {
            type: Boolean,
            value: false
        },
        //服务器地址
        serverUrl: {
            type: String,
            value: ""
        },
        //限制数
        limit: {
            type: Number,
            value: 9
        },
        //original 原图，compressed 压缩图，默认二者都有
        sizeType: {
            type: Array,
            value: ['original', 'compressed']
        },
        //album 从相册选图，camera 使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项
        sourceType: {
            type: Array,
            value: ['album', 'camera']
        },
        //可上传图片类型，默认为空，不限制  Array<String> [jpg,png,gif]
        imageFormat: {
            type: Array,
            value: []
        },
        //单张图片大小限制 MB
        size: {
            type: Number,
            value: 4
        },
        //文件对应的key，默认为 file
        fileKeyName: {
            type: String,
            value: "file"
        },
        //HTTP 请求 Header, header 中不能设置 Referer。
        header: {
            type: Object,
            value: {}
        },
        //HTTP 请求中其他额外的 form data
        formData: {
            type: Object,
            value: {}
        },
        //自定义参数
        params: {
            type: Number,
            optionalTypes: [String],
            value: 0
        }
    },
    lifetimes: {
        ready: function () {
            this.initImages();
            this.setData({
                currentUser: wx.getStorageSync('currentUser')
            })
        }
    },
    data: {
        //图片地址
        imageList: [],
        myIdList: [],
        //上传状态：1-上传成功 2-上传中 3-上传失败
        statusArr: [],
        currentUser: {},// 当前的用户信息
    },

    /**
     * 组件的方法列表
     */
    methods: {
        initImages() {
            let imgArr = [...this.data.value]
            let idList = [...this.data.idList]
            let status = []
            for (let item of imgArr) {
                status.push("1")
            }
            this.setData({
                imageList: [...imgArr],
                myIdList: [...idList],
                statusArr: status
            })
        },
        // 重新上传
        reUpLoad(e) {
            let index = Number(e.currentTarget.dataset.index)
            let value = `statusArr[${index}]`
            this.setData({
                [value]: "2"
            })
            this.change()
            this.uploadImage(index, this.data.imageList[index]).then(() => {
                this.change()
            }).catch(() => {
                this.change()
            })
        },
        /**
         * @param manual 是否手动上传
         **/
        change(manual = false) {
            let status = ~this.data.statusArr.indexOf("2") ? 2 : 1
            if (status != 2 && ~this.data.statusArr.indexOf("3")) {
                // 上传失败
                status = 3
            }
            this.triggerEvent('complete', {
                status: status,
                imgArr: this.data.imageList,
                idList: this.data.myIdList,
                params: this.data.params,
                manual: manual
            })
        },
        toast(text) {
            text && wx.showToast({
                title: text,
                icon: "none"
            });
        },
        chooseImage: function () {
            let _this = this;
            wx.chooseImage({
                count: _this.data.limit - _this.data.imageList.length,
                sizeType: _this.data.sizeType,
                sourceType: _this.data.sourceType,
                success: function (e) {
                    let imageArr = [];
                    let status = []
                    for (let i = 0; i < e.tempFiles.length; i++) {
                        let len = _this.data.imageList.length;
                        if (len >= _this.data.limit) {
                            _this.toast(`最多可上传${_this.data.limit}张图片`);
                            break;
                        }

                        //过滤图片类型
                        let path = e.tempFiles[i].path;

                        if (_this.data.imageFormat.length > 0) {
                            let format = path.split(".")[(path.split(".")).length - 1];
                            if (_this.data.imageFormat.indexOf(format) == -1) {
                                let text = `只能上传 ${_this.data.imageFormat.join(',')} 格式图片！`
                                _this.toast(text);
                                continue;
                            }
                        }

                        //过滤超出大小限制图片
                        let size = e.tempFiles[i].size;

                        if (_this.data.size * 1024 * 1024 < size) {
                            let err = `单张图片大小不能超过：${_this.data.size}MB`
                            _this.toast(err);
                            continue;
                        }

                        imageArr.push(path)
                        status.push("2")
                    }
                    _this.setData({
                        imageList: _this.data.imageList.concat(imageArr),
                        statusArr: _this.data.statusArr.concat(status)
                    })
                    _this.change()

                    let start = _this.data.imageList.length - imageArr.length
                    for (let j = 0; j < imageArr.length; j++) {
                        let index = start + j
                        //服务器地址
                        if (_this.data.serverUrl) {
                            _this.uploadImage(index, imageArr[j]).then(() => {
                                _this.change()
                            }).catch(() => {
                                _this.change()
                            })
                        } else {
                            //无服务器地址则直接返回成功
                            let value = `statusArr[${index}]`
                            _this.setData({
                                [value]: "1"
                            })
                            _this.change()
                        }
                    }
                }
            })
        },
        uploadImage: function (index, url, serverUrl) {
            let _this = this;
            let status = `statusArr[${index}]`;
            let {organizationName = ''} = _this.data.currentUser || {};
            let list = url.split('/')
            let fileName = list[list.length - 1];
            return new Promise((resolve, reject) => {
                // 请求授权
                authorizeOSSRenewWX({callbackUrl: this.data.serverUrl || serverUrl}).then(res => {
                    if (res.code !== 0) {
                        _this.setData({
                            [status]: "3"
                        })
                        reject(index)
                        return
                    }
                    let formData = {
                        key: `${organizationName}/image/${this.getCurDate()}/${fileName}`,
                        policy: res.data.accessPolicy,
                        OSSAccessKeyId: res.data.accessKeyId,
                        signature: res.data.accessSignature,
                        'x-oss-security-token': res.data.securityToken,
                        'callback': res.data.callback,
                        'success_action_status': '200',
                    };
                    let header = {'Content-Disposition': 'attachment;'};
                    wx.uploadFile({
                        url: res.data.httpsBucket || serverUrl,
                        name: _this.data.fileKeyName,
                        header,
                        formData,
                        filePath: url,
                        success: function (res) {
                            console.log(res)
                            if (res.statusCode == 200) {
                                //返回结果 此处需要按接口  实际返回进行修改
                                let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
                                //判断code，以实际接口规范判断
                                if (d.code % 100 === 0) {
                                    // 上传成功 d.url 为上传后图片地址，以实际接口返回为准
                                    let data = d.data || {}
                                    if (data.url || url) {
                                        let value = `imageList[${index}]`
                                        let id = `myIdList[${index}]`
                                        _this.setData({
                                            [value]: data.url || url,
                                            [id]: data.id || 0
                                        })
                                    }
                                    _this.setData({
                                        [status]: (data.url || url) ? "1" : "3"
                                    })
                                } else {
                                    // 上传失败
                                    _this.setData({
                                        [status]: "3"
                                    })
                                }
                                resolve(index)
                            } else {
                                _this.setData({
                                    [status]: "3"
                                })
                                reject(index)
                            }
                        },
                        fail: function (res) {
                            _this.setData({
                                [status]: "3"
                            })
                            reject(index)
                        }
                    })
                }).catch(() => {

                });
            })

        },
        delImage: function (e) {
            let index = Number(e.currentTarget.dataset.index)

            let imgList = [...this.data.imageList]
            let idList = [...this.data.myIdList]
            let status = [...this.data.statusArr]
            imgList.splice(index, 1)
            idList.splice(index, 1)
            status.splice(index, 1)
            this.setData({
                imageList: imgList,
                myIdList: idList,
                statusArr: status
            })
            this.triggerEvent("remove", {
                index: index,
                params: this.data.params
            })
            this.change()
        },
        previewImage: function (e) {
            let index = Number(e.currentTarget.dataset.index)
            if (!this.data.imageList.length) return;
            wx.previewImage({
                current: this.data.imageList[index],
                loop: true,
                urls: this.data.imageList
            })
        },
        /**
         * 当属性serverUrl传空时，父级调用该方法一次性上传所有图片
         * @param serverUrl 服务器接口地址
         **/
        uploadAllImage(serverUrl) {
            if (!serverUrl) {
                this.toast('服务器接口地址不能为空！');
                return;
            }
            let imageArr = [...this.data.imageList]
            const len = imageArr.length
            for (let i = 0; i < len; i++) {
                //如果是服务器地址图片则无需再次上传
                if (imageArr[i].startsWith('https')) {
                    continue;
                } else {
                    let status = `statusArr[${i}]`;
                    _this.setData({
                        [status]: "2"
                    })
                    this.uploadImage(i, imageArr[i], serverUrl).then(() => {
                        if (i === len - 1) {
                            this.change(true)
                        }
                    }).catch(() => {
                        if (i === len - 1) {
                            this.change(true)
                        }
                    })
                }
            }
        },
        getCurDate() {
            const newData = new Date()

            let y = newData.getFullYear(); //年份
            let m = newData.getMonth() + 1; //月份
            let d = newData.getDate(); //日期

            if (m >= 1 && m <= 9) {
                m = "0" + m;
            }
            if (d >= 1 && d <= 9) {
                d = "0" + d;
            }
            return y + "-" + m + "-" + d;
        }
    }
})